import random
import os
import sys

def clear_screen():
    """清屏函数，根据不同操作系统调用不同的清屏命令"""
    os.system('cls' if os.name == 'nt' else 'clear')

def wait_for_keypress():
    """等待用户按键，然后清屏"""
    if os.name == 'nt':  # Windows系统
        import msvcrt
        msvcrt.getch()
    else:  # Linux/Mac系统
        import termios
        fd = sys.stdin.fileno()
        old = termios.tcgetattr(fd)
        try:
            new = termios.tcgetattr(fd)
            new[3] = new[3] & ~termios.ICANON & ~termios.ECHO
            termios.tcsetattr(fd, termios.TCSADRAIN, new)
            sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old)
    clear_screen()

def get_player_count():
    """获取玩家人数并验证是否在3-12人范围内"""
    while True:
        try:
            player_count = int(input("请输入玩家人数（3-12人）："))
            if 3 <= player_count <= 12:
                return player_count
            else:
                print("人数必须在3-12人之间，请重新输入。")
        except ValueError:
            print("输入无效，请输入一个整数。")

def get_undercover_count(player_count):
    """根据玩家人数确定卧底数量"""
    if 3 <= player_count <= 4:
        return 1
    elif 5 <= player_count <= 7:
        return 2
    elif 8 <= player_count <= 10:
        return 3
    else:  # 11-12人
        return 4

def select_random_word_pair():
    """随机选择一组词语对"""
    word_pairs = [
        # 动物类
        ("猫", "狗"), ("狮子", "老虎"), ("大象", "长颈鹿"), 
        ("兔子", "老鼠"), ("熊猫", "熊"), ("金鱼", "鲤鱼"),
        ("老鹰", "秃鹫"), ("狼", "狐狸"), ("袋鼠", "树袋熊"),
        ("猴子", "猩猩"), ("孔雀", "凤凰"), ("鲨鱼", "鲸鱼"),
        ("蛇", "蜥蜴"), ("蜜蜂", "黄蜂"), ("蝴蝶", "蛾子"),
        ("蜗牛", "田螺"), ("蜘蛛", "蝎子"), ("青蛙", "蟾蜍"),
        ("乌鸦", "喜鹊"), ("鸽子", "斑鸠"), ("鹦鹉", "八哥"),
        ("绵羊", "山羊"), ("马", "驴"), ("骆驼", "单峰驼"),
        
        # 水果类
        ("苹果", "梨"), ("香蕉", "芭蕉"), ("草莓", "树莓"),
        ("葡萄", "提子"), ("西瓜", "哈密瓜"), ("橙子", "橘子"),
        ("菠萝", "凤梨"), ("猕猴桃", "奇异果"), ("芒果", "木瓜"),
        ("荔枝", "龙眼"), ("樱桃", "车厘子"), ("柚子", "文旦"),
        ("柠檬", "酸橙"), ("火龙果", "仙人掌果"), ("杨梅", "话梅"),
        ("椰子", "棕榈果"), ("柿子", "西红柿"), ("榴莲", "山竹"),
        
        # 食物类
        ("面包", "蛋糕"), ("包子", "饺子"), ("面条", "米线"),
        ("米饭", "粥"), ("汉堡", "三明治"), ("披萨", "馅饼"),
        ("薯条", "薯片"), ("饼干", "曲奇"), ("巧克力", "糖果"),
        ("冰淇淋", "冰棍"), ("火锅", "麻辣烫"), ("烧烤", "烤肉"),
        ("沙拉", "凉菜"), ("咖啡", "茶"), ("牛奶", "酸奶"),
        ("啤酒", "白酒"), ("红酒", "葡萄酒"), ("酱油", "老抽"),
        ("醋", "料酒"), ("糖", "盐"), ("面粉", "淀粉"),
        
        # 生活用品类
        ("牙刷", "牙膏"), ("毛巾", "浴巾"), ("香皂", "肥皂"),
        ("洗发水", "护发素"), ("梳子", "篦子"), ("镜子", "反光镜"),
        ("被子", "毯子"), ("枕头", "抱枕"), ("拖鞋", "凉鞋"),
        ("雨伞", "雨衣"), ("书包", "背包"), ("钱包", "皮夹"),
        ("手表", "手环"), ("眼镜", "墨镜"), ("手机", "电话"),
        ("电脑", "笔记本"), ("电视", "投影仪"), ("冰箱", "冰柜"),
        ("洗衣机", "烘干机"), ("吸尘器", "扫地机"), ("台灯", "吊灯"),
        
        # 自然现象类
        ("雨", "雪"), ("雷", "电"), ("风", "台风"),
        ("彩虹", "霓"), ("雾", "霾"), ("太阳", "月亮"),
        ("星星", "流星"), ("云", "霞"), ("山", "丘"),
        ("河", "江"), ("湖", "海"), ("沙漠", "戈壁"),
        ("森林", "树林"), ("草原", "草地"), ("火山", "地震"),
        ("龙卷风", "飓风"), ("极光", "极夜"), ("闪电", "雷光"),
        
        # 颜色类
        ("红色", "粉色"), ("黄色", "橙色"), ("蓝色", "紫色"),
        ("绿色", "青色"), ("黑色", "灰色"), ("白色", "银色"),
        ("金色", "铜色"), ("褐色", "棕色"), ("玫红", "紫红"),
        ("天蓝", "湖蓝"), ("草绿", "深绿"), ("橘红", "朱红"),
        ("米白", "乳白"), ("藏青", "深蓝"), ("橄榄绿", "军绿"),
        
        # 动作类
        ("跑步", "散步"), ("跳跃", "弹跳"), ("游泳", "潜水"),
        ("唱歌", "跳舞"), ("说话", "聊天"), ("吃饭", "喝水"),
        ("睡觉", "休息"), ("读书", "写字"), ("画画", "涂鸦"),
        ("开车", "骑车"), ("走路", "踏步"), ("爬山", "攀岩"),
        ("钓鱼", "打猎"), ("拍照", "摄影"), ("写字", "书法"),
        ("弹琴", "演奏"), ("打字", "按键"), ("微笑", "大笑"),
        
        # 身体部位类
        ("眼睛", "眉毛"), ("鼻子", "嘴巴"), ("耳朵", "耳垂"),
        ("头发", "胡子"), ("手", "脚"), ("手指", "脚趾"),
        ("胳膊", "腿"), ("肩膀", "后背"), ("肚子", "腹部"),
        ("心脏", "肝脏"), ("肺", "胃"), ("大脑", "小脑"),
        ("皮肤", "汗毛"), ("牙齿", "舌头"), ("睫毛", "眼睑"),
        
        # 职业类
        ("医生", "护士"), ("老师", "学生"), ("警察", "保安"),
        ("厨师", "服务员"), ("司机", "乘客"), ("演员", "导演"),
        ("歌手", "乐队"), ("作家", "编辑"), ("画家", "设计师"),
        ("程序员", "工程师"), ("农民", "工人"), ("军人", "士兵"),
        ("记者", "摄影师"), ("律师", "法官"), ("会计", "出纳"),
        
        # 交通工具类
        ("汽车", "火车"), ("飞机", "轮船"), ("自行车", "电动车"),
        ("公交车", "出租车"), ("地铁", "轻轨"), ("卡车", "货车"),
        ("摩托车", "三轮车"), ("直升机", "客机"), ("帆船", "游艇"),
        ("潜艇", "邮轮"), ("缆车", "索道"), ("雪橇", "雪车"),
        ("坦克", "装甲车"), ("消防车", "救护车"), ("高铁", "动车"),
        
        # 抽象概念类
        ("快乐", "幸福"), ("悲伤", "难过"), ("愤怒", "生气"),
        ("喜欢", "爱"), ("梦想", "理想"), ("时间", "空间"),
        ("自由", "解放"), ("生命", "生活"), ("金钱", "财富"),
        ("友谊", "爱情"), ("和平", "战争"), ("困难", "挑战"),
        ("成功", "失败"), ("智慧", "聪明"), ("勇气", "胆量"),
        ("美丽", "漂亮"), ("善良", "仁慈"), ("诚实", "诚信"),
        ("希望", "期待"), ("信任", "怀疑"), ("真理", "真相"),
        
        # 体育类
        ("篮球", "足球"), ("乒乓球", "羽毛球"), ("游泳", "跳水"),
        ("跑步", "竞走"), ("跳高", "跳远"), ("举重", "摔跤"),
        ("拳击", "跆拳道"), ("网球", "壁球"), ("排球", "沙滩排球"),
        ("棒球", "垒球"), ("高尔夫", "保龄球"), ("滑雪", "滑冰"),
        ("射箭", "射击"), ("体操", "艺术体操"), ("击剑", "花剑"),
        
        # 音乐类
        ("钢琴", "小提琴"), ("吉他", "贝斯"), ("鼓", "架子鼓"),
        ("唱歌", "说唱"), ("流行", "摇滚"), ("古典", "爵士"),
        ("二胡", "古筝"), ("笛子", "唢呐"), ("琵琶", "扬琴"),
        ("交响乐", "协奏曲"), ("歌词", "旋律"), ("高音", "低音"),
        ("节奏", "节拍"), ("和声", "和弦"), ("演唱会", "音乐会"),
        
        # 科技类
        ("手机", "平板"), ("电脑", "服务器"), ("相机", "摄像机"),
        ("网络", "互联网"), ("软件", "硬件"), ("程序", "代码"),
        ("人工智能", "机器学习"), ("大数据", "云计算"), ("游戏", "电竞"),
        ("微信", "QQ"), ("微博", "博客"), ("淘宝", "京东"),
        ("抖音", "快手"), ("电脑", "笔记本"), ("耳机", "音响"),
        ("蓝牙", "WiFi"), ("电池", "电源"), ("屏幕", "显示器"),
        
        # 地理类
        ("北京", "上海"), ("纽约", "伦敦"), ("东京", "首尔"),
        ("长江", "黄河"), ("泰山", "华山"), ("太平洋", "大西洋"),
        ("亚洲", "欧洲"), ("沙漠", "绿洲"), ("岛屿", "半岛"),
        ("城市", "乡村"), ("山脉", "高原"), ("平原", "盆地"),
        ("海洋", "湖泊"), ("河流", "运河"), ("森林", "雨林"),
        
        # 数学类
        ("加法", "减法"), ("乘法", "除法"), ("方程", "函数"),
        ("几何", "代数"), ("直线", "曲线"), ("圆形", "椭圆"),
        ("三角形", "四边形"), ("角度", "弧度"), ("面积", "体积"),
        ("正数", "负数"), ("整数", "小数"), ("分数", "百分数"),
        ("概率", "统计"), ("导数", "积分"), ("矩阵", "行列式"),
        
        # 历史类
        ("秦朝", "汉朝"), ("唐朝", "宋朝"), ("明朝", "清朝"),
        ("皇帝", "皇后"), ("宰相", "大臣"), ("将军", "士兵"),
        ("战争", "和平"), ("起义", "革命"), ("条约", "协议"),
        ("故宫", "长城"), ("金字塔", "兵马俑"), ("孔子", "孟子"),
        ("李白", "杜甫"), ("秦始皇", "汉武帝"), ("李世民", "赵匡胤"),
        
        # 游戏类
        ("象棋", "围棋"), ("扑克", "麻将"), ("跳棋", "军棋"),
        ("三国杀", "狼人杀"), ("英雄联盟", "王者荣耀"), ("吃鸡", "APEX"),
        ("超级玛丽", "塞尔达"), ("俄罗斯方块", "贪吃蛇"), ("扫雷", "纸牌"),
        ("我的世界", "迷你世界"), ("第五人格", "黎明杀机"), ("炉石传说", "万智牌"),
        ("宝可梦", "数码宝贝"), ("动物森友会", "星露谷物语"), ("文明", "帝国时代"),
        
        # 影视类
        ("电影", "电视剧"), ("演员", "角色"), ("导演", "编剧"),
        ("喜剧", "悲剧"), ("动作", "科幻"), ("爱情", "悬疑"),
        ("漫威", "DC"), ("哈利波特", "指环王"), ("复仇者联盟", "正义联盟"),
        ("蜘蛛侠", "超人"), ("蝙蝠侠", "钢铁侠"), ("孙悟空", "猪八戒"),
        ("白蛇传", "青蛇传"), ("泰坦尼克号", "阿凡达"), ("侏罗纪公园", "星球大战"),
        
        # 神话传说类
        ("孙悟空", "六耳猕猴"), ("哪吒", "二郎神"), ("嫦娥", "玉兔"),
        ("龙王", "龙女"), ("玉皇大帝", "王母娘娘"), ("如来佛祖", "观音菩萨"),
        ("牛郎", "织女"), ("许仙", "白素贞"), ("梁山伯", "祝英台"),
        ("盘古", "女娲"), ("夸父", "后羿"), ("精卫", "刑天"),
        ("孙悟空", "杨戬"), ("猪八戒", "沙僧"), ("红孩儿", "哪吒"),
        
        # 节日类
        ("春节", "除夕"), ("中秋节", "元宵节"), ("端午节", "清明节"),
        ("圣诞节", "平安夜"), ("情人节", "七夕节"), ("万圣节", "鬼节"),
        ("元旦", "新年"), ("劳动节", "国庆节"), ("儿童节", "青年节"),
        ("母亲节", "父亲节"), ("感恩节", "复活节"), ("愚人节", "情人节"),
        
        # 其他类
        ("学校", "幼儿园"), ("医院", "诊所"), ("超市", "便利店"),
        ("图书馆", "书店"), ("公园", "动物园"), ("银行", "邮局"),
        ("酒店", "旅馆"), ("电影院", "剧院"), ("商场", "购物中心"),
        ("警察局", "消防站"), ("机场", "火车站"), ("博物馆", "展览馆"),
        ("餐厅", "饭店"), ("咖啡厅", "茶馆"), ("健身房", "游泳馆"),
    ]
    
    return random.choice(word_pairs)

def assign_roles(player_count, undercover_count, civilian_word, undercover_word):
    """分配角色和词语"""
    roles = ['平民'] * (player_count - undercover_count) + ['卧底'] * undercover_count
    random.shuffle(roles)
    
    words = []
    for role in roles:
        if role == '平民':
            words.append(civilian_word)
        else:
            words.append(undercover_word)
    
    return roles, words

def main():
    clear_screen()
    print("欢迎来到谁是卧底游戏！")
    print("游戏规则：跳过描述环节，直接进入投票环节")
    
    player_count = get_player_count()
    undercover_count = get_undercover_count(player_count)
    
    # 随机选择词语对
    civilian_word, undercover_word = select_random_word_pair()
    
    # 显示选择的词语对
    print(f"\n已为您随机选择的词语对：")
    print(f"平民词：{civilian_word}")
    print(f"卧底词：{undercover_word}")
    
    input("\n按任意键开始游戏...")
    clear_screen()
    
    roles, words = assign_roles(player_count, undercover_count, civilian_word, undercover_word)
    
    # 让每个玩家查看自己的词语
    for i in range(player_count):
        print(f"现在是玩家 {i+1} 查看词语")
        print(f"你的身份是：{roles[i]}")
        print(f"你的词语是：{words[i]}")
        input("\n按任意键继续...")
        clear_screen()
    
    # 显示所有玩家的词语和身份
    print("\n所有玩家的词语和身份信息：")
    for i in range(player_count):
        print(f"玩家 {i+1}: 身份-{roles[i]}, 词语-{words[i]}")
    
    print("\n游戏结束！")

if __name__ == "__main__":
    main()    
